var app = getApp()
Page({
  data: {
    selectAllStatus: false
  },
  // 增加数量
  add(e) {
    var url = app.data.url
    const index = e.currentTarget.dataset.index;
    const cartId = e.currentTarget.dataset.cart;
    const goodsid = e.currentTarget.dataset.goodsid;
    let carts = this.data.shoplist;
    let num = carts[index].Number;
    num = num + 1;
    carts[index].Number = num;
    this.setData({
      shoplist: carts
    });
    this.getTotalPrice();
    wx.request({
      url: '' + url + '/shop/shoppingcart/modifynum',
      data: {
        cartId: cartId,
        goodsid: goodsid,
        num: num
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res)
      },
      error: function (res) {
        console.log(res)
      }
    })
  },
  // 减少数量
  minus(e) {
    var url = app.data.url
    const index = e.currentTarget.dataset.index;
    const cartId = e.currentTarget.dataset.cart;
    const goodsid = e.currentTarget.dataset.goodsid;
    let carts = this.data.shoplist;
    let num = carts[index].Number;
    if (num <= 1) {
      return false;
    }
    num = num - 1;
    carts[index].Number = num;
    this.setData({
      shoplist: carts
    });
    this.getTotalPrice();
    wx.request({
      url: '' + url + '/shop/shoppingcart/modifynum',
      data: {
        cartId: cartId,
        goodsid: goodsid,
        num: num
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res)
      },
      error: function (res) {
        console.log(res)
      }
    })
  },
  //单选
  selectList(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.shoplist;
    const selected = carts[index].selected;
    if (selected) {
      carts[index].selected = !selected;
      this.setData({
        shoplist: carts
      });
      this.getTotalPrice();
    } else {
      carts[index].selected = true;
      this.setData({
        shoplist: carts
      });
      this.getTotalPrice();
    }
  },
  //全选
  bindSelectAll: function (e) {
    let selectAllStatus = this.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;
    let carts = this.data.shoplist;
    for (let i = 0; i < carts.length; i++) {
      carts[i].selected = selectAllStatus;
    }
    this.setData({
      selectAllStatus: selectAllStatus,
      shoplist: carts
    });
    this.getTotalPrice();
  },
  //总价
  getTotalPrice() {
    let carts = this.data.shoplist;
    let total = 0;
    let num = 0
    for (let i = 0; i < carts.length; i++) {
      if (carts[i].selected) {
        total += carts[i].Number * carts[i].Price;
        num++
      }
    }
    console.log(total, num)
    this.setData({
      carts: carts,
      totalPrice: total.toFixed(2)
    });
  },
  //删除
  del: function (e) {
    var id = e.currentTarget.id
    var url = app.data.url
    wx.request({
      url: '' + url + '/shop/shoppingcart/remove',
      data: { id: id },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            complete: function () {
              wx.switchTab({
                url: '../shopping-cart/shopping-cart',
              })
            }
          })
        }
      },
      error: function (res) {
        console.log(res)
      }
    })
  },
  creatOrder: function () {
    let carts = this.data.shoplist;
    let data = ""
    for (let i = 0; i < carts.length; i++) {
      if (carts[i].selected) {
        if (data) {
          data += ",";
        }
        data += '{"goodsid":' + carts[i].GoodsId + ',"num":' + carts[i].Number + ',"cartid":' + carts[i].Id + '}';
      }
    }
    var newData = '[' + data + ']'
    wx.redirectTo({
      url: '../confirm-order/confirm-order?data=' + newData + '',
    })
  },
  onLoad: function () {
    var url = app.data.url
    var that = this
    //购物车列表
    wx.request({
      url: '' + url + '/shop/shoppingcart/list',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if (res.data.Rows.length > 0) {
          var shoplist = res.data.Rows
          let carts = shoplist;
          for (let i = 0; i < carts.length; i++) {
            carts[i].selected = false
          }
          that.setData({
            shoplist: carts,
            url: url
          })
          that.getTotalPrice()
        }
      },
      error: function (res) {
        console.log(res)
      }
    })
  }
})